package br.edu.senai.view;

import br.edu.senai.controller.ClienteDAO;
import br.edu.senai.controller.MedicaoDAO;
import br.edu.senai.controller.MedicaoDAOP;
import br.edu.senai.controller.ParametroDAO;
import br.edu.senai.controller.ResultadoDAO;
import br.edu.senai.model.Cliente;
import br.edu.senai.model.Medicao;
import br.edu.senai.model.Mes;
import br.edu.senai.model.Parametro;
import br.edu.senai.model.Resultado;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.extensions.model.dynaform.DynaFormControl;
import org.primefaces.extensions.model.dynaform.DynaFormLabel;
import org.primefaces.extensions.model.dynaform.DynaFormModel;
import org.primefaces.extensions.model.dynaform.DynaFormRow;

@ManagedBean
@ViewScoped
public class MedicaoBean implements Serializable {

    private static final long serialVersionUID = 20120423L;
    private DynaFormModel model;
    private final ResultadoDAO rDAO = new ResultadoDAO();
    private final ClienteDAO cDAO = new ClienteDAO();
    private final ParametroDAO pDAO = new ParametroDAO();
    private final MedicaoDAOP mDAOP = new MedicaoDAOP();
    private Cliente cliente = new Cliente();
    private Mes mesSel;

    public Cliente getCliente() {
        return cliente;
    }

    public String setCliente(Cliente cliente) {
        this.cliente = cliente;
        return "medicao";
    }

    public Mes getMesSel() {
        return mesSel;
    }

    public void setMesSel(Mes mesSel) {
        this.mesSel = mesSel;
    }

    /*Aqui método que acesso no selectOneMenu*/
    public Mes[] getMeses() {
        return Mes.values();
    }

    @PostConstruct
    protected void initialize() {

        model = new DynaFormModel();

        DynaFormRow row = model.createRegularRow();
        DynaFormLabel label21 = row.addLabel("Ano");
        DynaFormControl control22 = row.addControl(new MedicaoDAO("Ano", true, -3L), "ano");
        label21.setForControl(control22);
        
        row = model.createRegularRow();
        DynaFormLabel label41 = row.addLabel("Mês");
        DynaFormControl control42 = row.addControl(new MedicaoDAO("Mês", true, -1L), "mes");
        label41.setForControl(control42);

        row = model.createRegularRow();
        DynaFormLabel label31 = row.addLabel("Cliente");
        DynaFormControl control32 = row.addControl(new MedicaoDAO("Cliente", true, -2L), "cliente");
        label31.setForControl(control32);

        //criar método no parametro dao para listar apenas os parametros do cliente. hoje traz todos
        for (Parametro parametro : pDAO.listar()) {
            row = model.createRegularRow();
            DynaFormLabel label11 = row.addLabel(parametro.getNome());
            DynaFormControl control12 = row.addControl(new MedicaoDAO(parametro.getNome(), false,
                    parametro.getId()), "input");
            label11.setForControl(control12);
        }
    }

    public DynaFormModel getModel() {
        return model;
    }

    public List<MedicaoDAO> getMedicaoProperties() {
        if (model == null) {
            return null;
        }
        List<MedicaoDAO> medicaoProperties = new ArrayList<>();
        for (DynaFormControl dynaFormControl : model.getControls()) {
            medicaoProperties.add((MedicaoDAO) dynaFormControl.getData());
        }
        return medicaoProperties;
    }

    public String submitForm() {
        FacesMessage.Severity sev = FacesContext.getCurrentInstance().getMaximumSeverity();
        boolean hasErrors = (sev != null && (FacesMessage.SEVERITY_ERROR.compareTo(sev) >= 0));

        RequestContext requestContext = RequestContext.getCurrentInstance();
        requestContext.addCallbackParam("isValid", !hasErrors);

        Medicao medicao = new Medicao();
        List<Resultado> lResultados = new ArrayList<>();

        for (DynaFormControl dynaFormControl : model.getControls()) {
            MedicaoDAO m = (MedicaoDAO) dynaFormControl.getData();

            if(m.getId() == -1) {
                medicao.setMes(Mes.valueOf(String.valueOf(m.getValue())));
            }
            else if (m.getId() == -2) {
                medicao.setCliente(cDAO.find(Long.parseLong(String.valueOf(m.getValue()))));
            }
            else if (m.getId() == -3) {
                medicao.setAno(Integer.parseInt(String.valueOf(m.getValue())));
            } else {
                Resultado r = new Resultado();
                r.setMedicao(medicao);
                r.setParametro(pDAO.find(m.getId()));
                r.setValor(Double.parseDouble(String.valueOf(m.getValue())));
                lResultados.add(r);
            }
        }

        medicao.setResultados(lResultados);
        mDAOP.salvar(medicao);

        return null;
    }
}
